package com.rceeslca.modules.cam.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rceeslca.common.exception.RceesLcaException;
import com.rceeslca.common.service.impl.BaseServiceImpl;
import com.rceeslca.modules.cam.dao.CamCourseDao;
import com.rceeslca.modules.cam.dao.CamUserDao;
import com.rceeslca.modules.cam.entity.CamCourse;
import com.rceeslca.modules.cam.service.CamCourseService;
import com.rceeslca.modules.cam.vo.CamCourseVo;
import com.rceeslca.modules.cam.vo.CamUserVo;
import com.rceeslca.modules.security.user.UserDetail;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程
 */
@Service
@AllArgsConstructor
public class CamCourseServiceImpl extends BaseServiceImpl<CamCourseDao, CamCourse> implements CamCourseService {

    @Autowired
    private CamUserDao camUserDao;


    /**
     * 创建
     * @param course 课程
     */
    @Override
    public void createCourse(CamCourse course) {
        insert(course);
    }


    /**
     * 删除
     * @param course 课程
     */
    @Override
    public void deleteCourse(CamCourse course) {
        //存在
        CamCourse entity = baseDao.selectById(course.getId());
        if(entity==null){
            throw new RceesLcaException("数据不存在");
        }else{
            deleteById(course);
        }
    }


    /**
     * 更改
     * @param course 课程
     */
    @Override
    public void updateCourse(CamCourse course) {
        //存在
        CamCourse entity = baseDao.selectById(course.getId());
        if(entity==null){
            throw new RceesLcaException("数据不存在");
        }else{
            entity.setName(course.getName());
            entity.setTeacherId(course.getTeacherId());
            entity.setClassRoom(course.getClassRoom());
            entity.setClassTime(course.getClassTime());
            entity.setUpdater(course.getUpdater());
            entity.setUpdateDate(course.getUpdateDate());
            updateById(entity);
        }
    }


    /**
     * 列展示
     * @param param 参数
     */
    @Override
    public List<CamCourseVo> listCourse(CamCourseVo param) {
        return baseDao.listSelect(param.getName());
    }


    /**
     * 分页查询
     * @param page 分页
     * @param param 参数
     */
    @Override
    public IPage<CamCourseVo> pageList(Page<CamCourseVo> page, CamCourseVo param) {

        //当前用户
        UserDetail user = (UserDetail) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        List<CamUserVo> userVoList = camUserDao.listUser(null, user.getId());
        if(userVoList!=null&&!userVoList.isEmpty()){
            CamUserVo camUserVo = userVoList.get(0);

            if(camUserVo.getRoleName()!=null){
                if(camUserVo.getRoleName().equals("教师")){
                    return baseDao.pageSelect(page,param.getName(),camUserVo.getId(),param.getTeacherName());
                }else if(camUserVo.getRoleName().equals("管理员")){
                    return baseDao.pageSelect(page,param.getName(),null,param.getTeacherName());
                }else{
                    return null;
                }
            }else{
                if(camUserVo.getSuperAdmin()==1){
                    return baseDao.pageSelect(page,param.getName(),null,param.getTeacherName());
                }else{
                    return null;
                }
            }
        }else{
            return null;
        }
    }

}
